﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;

namespace netcore_service.Model
{

    // Models/User.cs
    public class User
    {
        public string Id { get; set; } = Guid.NewGuid().ToString();
        public string UserName { get; set; } = string.Empty;
        public string Email { get; set; } = string.Empty;
        public UserRole Role { get; set; }
        public bool IsOnline { get; set; }
        public DateTime LastSeen { get; set; }
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

        // 导航属性
        public virtual ICollection<ChatSession> CustomerSessions { get; set; } = new List<ChatSession>();
        public virtual ICollection<ChatSession> AgentSessions { get; set; } = new List<ChatSession>();
        public virtual ICollection<Message> Messages { get; set; } = new List<Message>();
    }

    public enum UserRole
    {
        Customer,
        Agent,
        Admin
    }

    // Models/ChatSession.cs
    public class ChatSession
    {
        [SugarColumn(IsPrimaryKey =true)]
        public string Id { get; set; } = Guid.NewGuid().ToString();
        public string CustomerId { get; set; } = string.Empty;
        public string CustomerName { get; set; } = string.Empty;
        public string? AgentId { get; set; }
        public string? AgentName { get; set; }
        public SessionStatus Status { get; set; }
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
        public DateTime? EndedAt { get; set; }
        public int MessageCount { get; set; }
        public DateTime? LastMessageTime { get; set; }
        public string School { get; set; } = string.Empty;
        public string SchoolName { get; set; } = string.Empty;
        public string Logo { get; set; } = string.Empty;

        // 导航属性
        [SugarColumn(IsIgnore =true)]
        public virtual User Customer { get; set; } = null!;
        [SugarColumn(IsIgnore = true)]
        public virtual User? Agent { get; set; }
        [SugarColumn(IsIgnore = true)]
        public virtual ICollection<Message> Messages { get; set; } = new List<Message>();
    }

    public enum SessionStatus
    {
        Waiting,    // 等待客服接入
        Active,     // 进行中
        Closed      // 已结束
    }

    // Models/Message.cs
    public class Message
    {
        [SugarColumn(IsPrimaryKey = true)]
        public string Id { get; set; } = Guid.NewGuid().ToString();
        public string SessionId { get; set; } = string.Empty;
        public string SenderId { get; set; } = string.Empty;
        public string SenderName { get; set; } = string.Empty;
        public string Content { get; set; } = string.Empty;
        public MessageType MessageType { get; set; } = MessageType.Text;
        public DateTime SentAt { get; set; } = DateTime.UtcNow;
        public bool IsRead { get; set; }
        public string Logo { get; set; } = string.Empty;

        // 导航属性
        [SugarColumn(IsIgnore = true)]
        public virtual ChatSession Session { get; set; } = null!;
        [SugarColumn(IsIgnore = true)]
        public virtual User Sender { get; set; } = null!;
    }

    public enum MessageType
    {
        Text,
        Image,
        File,
        System
    }

    // Models/DTOs
    public class CreateSessionRequest
    {
        public string CustomerId { get; set; } = string.Empty;
        public string? InitialMessage { get; set; }
    }

    public class SendMessageRequest
    {
        public string SessionId { get; set; } = string.Empty;
        public string Content { get; set; } = string.Empty;
        public MessageType MessageType { get; set; } = MessageType.Text;
    }

    public class MessageDto
    {
        public string Id { get; set; } = string.Empty;
        public string SessionId { get; set; } = string.Empty;
        public string SenderId { get; set; } = string.Empty;
        public string SenderName { get; set; } = string.Empty;
        public string Content { get; set; } = string.Empty;
        public MessageType MessageType { get; set; }
        public DateTime SentAt { get; set; }
        public bool IsRead { get; set; }
        public string Logo { get; set; } = string.Empty;
    }

    public class SessionDto
    {
        public string Id { get; set; } = string.Empty;
        public string CustomerId { get; set; } = string.Empty;
        public string CustomerName { get; set; } = string.Empty;
        public string? AgentId { get; set; }
        public string? AgentName { get; set; }
        public SessionStatus Status { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime? EndedAt { get; set; }
        public int MessageCount { get; set; }
        public DateTime? LastMessageTime { get; set; }
        public MessageDto? LastMessage { get; set; }
    }

}
